package controller;

import model.Network;
import model.connectible.ISP;
import model.connectible.Peer;

public class MainUpdater implements Runnable{
	
	private Network network;
	private boolean running;
	private FileTransferService fileTransferService;
	
	public MainUpdater(Network network, FileTransferService fileTransferService) {
		setNetwork(network);
		setFileTransferService(fileTransferService);
		setRunning(true);
//		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//		Date date = new Date();
//		try {
//			BufferedWriter writer = new BufferedWriter(new FileWriter("data"+dateFormat.format(date)+".txt"));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println(network.toString());
	}

	public Network getNetwork() {
		return network;
	}

	public void setNetwork(Network network) {
		this.network = network;
	}
	
	public boolean isRunning() {
		return running;
	}

	public void setRunning(boolean running) {
		this.running = running;
	}
	
	public FileTransferService getFileTransferService() {
		return fileTransferService;
	}

	public void setFileTransferService(FileTransferService fileTransferService) {
		this.fileTransferService = fileTransferService;
	}

	@Override
	public void run() {
		/*
		 * Main loop. First prototype.
		 */
//		for (int k = 0; k < 2; k++) {
		int counter = 0;
		while(isRunning()) {
//			System.out.println(counter++);
			setRunning(false);
			for(int i = 0; i < network.getAllPeerList().size(); i++) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Peer peer = network.getAllPeerList().get(i);
				peer.pushTimer();
//				System.out.println();
//				System.out.println("Peer nr "+peer.getId());

				if(peer.getWantedFile()!=null) {
//					System.out.println("File id: "+peer.getWantedFile().getId());	
//					System.out.println("Downloaded: "+peer.getWantedFile().isDownloaded());
//					System.out.println("Downloaded size: "+peer.getWantedFile().getDownloadedSize());
				}
//				System.out.println();
				
				uncheckAllISP();
				if(peer.getWantedFile() != null) {
					if(peer.getRoute() == null) {
//						System.out.println("Request sent");
						fileTransferService.sendRequest(peer);
					} else if(!peer.getRoute().getWantedFile().isDownloaded()){
//						System.out.println("file update");
						peer.getRoute().stepForward();
					}
					if (peer.getRoute() != null) {
						setRunning(isRunning() || !peer.getRoute().getWantedFile().isDownloaded());
					} else {
						setRunning(isRunning() || false);
					}
				}
			}
		}
		System.out.println("Average distance: "+fileTransferService.getAverageJumpCount());
		System.out.println("Average stability: "+fileTransferService.getAverageStability());
		System.out.println("Average transfer: "+fileTransferService.getAverageTransfer());
		System.out.println("Local movement percent: "+fileTransferService.getLocalMovementPercent()+"%");
	}

	public void uncheckAllISP() {
		for (ISP isp : network.getAllISPList()) {
			isp.setChecked(false);
		}
	}
}
